﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gunningine
{
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Globalization;

    internal class FontLoader : IResourceLoader
    {
        #region IResourceLoader implementation.

        public string Name
        {
            get { return "Font"; }
        }

        public string[] SupportedFormats
        {
            get { return new string[] { ".fnt" }; }
        }

        /// <summary>
        /// Load font into memory.
        /// </summary>
        public void Load(Resource res)
        {
            FontResource fontResource = (FontResource)res;

            fontResource.Size = 0;

            // Load definition file and create glyphs.
            foreach (string line in File.ReadLines(fontResource.Filename))
            {
                string curLine = line.Trim();

                // Ignore comments.
                if (curLine.StartsWith("#"))
                {
                    continue;
                }
                else if (curLine.StartsWith("Bitmap"))
                {
                    fontResource.Image = curLine.Substring(curLine.IndexOf("=") + 1).Trim();
                }
                else if (curLine.StartsWith("Char"))
                {
                    bool hexChar;
                    string pattern;

                    if (curLine.Contains('"'))
                    {
                        hexChar = false;
                        pattern = "Char=\"(?<CHAR>.*)\",(?<XPOS>.*),(?<YPOS>.*),(?<WIDTH>.*),(?<HEIGHT>.*),(?<KERN>.*),";
                    }
                    else
                    {
                        hexChar = true;
                        pattern = @"Char=\s*(?<CHAR>.+),(?<XPOS>.*),(?<YPOS>.*),(?<WIDTH>.*),(?<HEIGHT>.*),(?<KERN>.*),";
                    }

                    foreach (Match match in Regex.Matches(curLine, pattern))
                    {
                        int chr = hexChar ? Int32.Parse(match.Groups["CHAR"].ToString(), NumberStyles.HexNumber)
                                            : (int)(match.Groups["CHAR"].ToString()[0]);

                        fontResource.Glyphs[chr].x = Int32.Parse(match.Groups["XPOS"].ToString());
                        fontResource.Glyphs[chr].y = Int32.Parse(match.Groups["YPOS"].ToString());
                        fontResource.Glyphs[chr].w = Int32.Parse(match.Groups["WIDTH"].ToString());
                        fontResource.Glyphs[chr].h = Int32.Parse(match.Groups["HEIGHT"].ToString());
                        fontResource.Glyphs[chr].kern = Int32.Parse(match.Groups["KERN"].ToString());

                        if (fontResource.Glyphs[chr].h > fontResource.Size)
                            fontResource.Size = fontResource.Glyphs[chr].h;
                    }
                }
            }
        }

        #endregion
    }
}
